package unq.tpi.persistencia.alumnos.dao.hbm;

import org.hibernate.Query;
import org.hibernate.Session;

import unq.tpi.persistencia.alumnos.arq.runner.HibernateRunner;
import unq.tpi.persistencia.alumnos.dao.MateriaDAO;
import unq.tpi.persistencia.alumnos.model.Materia;

public class MateriaDAOHbm implements MateriaDAO {

	public Materia getPorCodigo(String codigoMateria) {
		//throw new RuntimeException("Implement me :(");
 
		Query q=HibernateRunner.currentSession()
				.createQuery("FROM Materia WHERE codigo = ?");
		q.setString(0,codigoMateria);
		return (Materia) q.uniqueResult();
	}

	public void save(Materia materia) {
		HibernateRunner.currentSession().save(materia);
	}

	@Override
	public Integer getCantidadAprobados(String codigoMateria) {
		//throw new RuntimeException("Implement me :(");
		Session s=HibernateRunner.currentSession();
		Query q2=s.createQuery("SELECT count(*) FROM Cursada as c WHERE c.nota >= ? AND c.materia.codigo = ?");
		q2.setInteger(0,7);
		q2.setString(1,codigoMateria);
		return ((Long)q2.uniqueResult()).intValue();
	}
}
